package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.HomeBannerBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryPageResourceRes;
import sz.szsmk.citizencard.util.GlideRoundTransform;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/6/11 20:20
 * description:
 */
public class BannerAdapter extends DelegateAdapter.Adapter<BannerAdapter.BannerViewHolder> implements OnBannerListener {
    private Context mContext;
    private DefaultLayoutHelper mDefaultLayoutHelper;
    private ArrayList<String> iconList = new ArrayList<>();
    private Integer[] images;
    private QueryPageResourceRes viewData;
    private QueryPageResourceRes.ResourceListBean listBean;

    SmkBanner smkBanner;



    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int needHeader, int resource_id,int modularid);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    private BaseInterface baseInterface;

    public BannerAdapter(Context ins, DefaultLayoutHelper bannerLayouHelper) {
        mContext = ins;
        mDefaultLayoutHelper = bannerLayouHelper;

    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mDefaultLayoutHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.home_banner;
    }

    @Override
    public int getItemCount() {
        return 1;
    }


    @Override
    public BannerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return BannerViewHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(BannerViewHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(final int position, HomeBannerBinding binding) {

        if (viewData != null && viewData.getResource_list() != null && viewData.getResource_list().size() > 0) {
            iconList.clear();
            for (int i = 0; i < viewData.getResource_list().size(); i++) {
                viewData.getResource_list();
                String resource_url = viewData.getResource_list().get(i).getResource_url();
                iconList.add(resource_url);
            }

            Banner homeBanner = binding.banner;
            homeBanner.setLoopTime(4000);
            if(homeBanner.getAdapter()==null){
                homeBanner.setAdapter(smkBanner = new SmkBanner(iconList));
            }
            homeBanner.setIndicator(new CircleIndicator(mContext));
            homeBanner.getAdapter().setDatas(iconList);
            homeBanner.getAdapter().notifyDataSetChanged();
            homeBanner.setOnBannerListener(this);

        }
    }
    @Override
    public void OnBannerClick(Object data, int position) {
        QueryPageResourceRes.ResourceListBean resourceListBean = viewData.getResource_list().get(position);
        if (resourceListBean != null) {

            String link_url = resourceListBean.getLink_url();
            int need_header = resourceListBean.getNeed_header();
            String sys_label = resourceListBean.getSys_label();
            int modularid = resourceListBean.getModularId();
            if (baseInterface != null) {
                baseInterface.onClick(null, link_url, sys_label, need_header,0,modularid);
            }
        }
    }

    public void setViewData(QueryPageResourceRes viewData) {
        if (viewData != null) {
            this.viewData = viewData;
            notifyDataSetChanged();
        }
    }

    static class BannerViewHolder extends RecyclerView.ViewHolder {
        private HomeBannerBinding binding;

        public static BannerViewHolder creat(ViewGroup parent) {
            HomeBannerBinding binding = HomeBannerBinding
                    .inflate(LayoutInflater.from((parent.getContext())), parent, false);
            return new BannerViewHolder(binding);
        }

        public BannerViewHolder(HomeBannerBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public class SmkBanner extends com.youth.banner.adapter.BannerAdapter<String, SmkBanner.BannerViewHolder>{


        public SmkBanner(List<String> datas) {
            super(datas);
        }

        @Override
        public SmkBanner.BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            ImageView imageView = new ImageView(parent.getContext());
            //注意，必须设置为match_parent，这个是viewpager2强制要求的
            imageView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType( ImageView.ScaleType.CENTER_CROP);
            return new SmkBanner.BannerViewHolder(parent.getContext() ,imageView,imageView);
        }

        @Override
        public void onBindView(SmkBanner.BannerViewHolder holder, String data, int position, int size) {
            Glide.with(mContext).load(data)
                    .transform(new GlideRoundTransform(mContext, 10)).into(holder.iv);
        }

        class BannerViewHolder extends ViewHolder{

            ImageView iv;

            public BannerViewHolder(Context context, View itemView,ImageView iv) {
                super(context, itemView);
                this.iv = iv;
            }
        }
    }


}
